package com.softeem.springboot_01.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.springboot_01.entity.User;
import com.softeem.springboot_01.mapper.UserMapper;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Wrapper;
import java.util.Objects;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/login")
    public String login(String username,String password){
//        1.根据用户名查询用户是否存在
        QueryWrapper<User> query= Wrappers.query(User.class);
        query.eq("username",username);

        //根据用户名查询该用户是否存在 select* from user where username=?
        User user = userMapper.selectOne(query);
        if(Objects.nonNull(user)){
            //        2.若存在则判断密码是否存在
            //        3.判断输入的密码是否和数据库一致
            //        4.若一致则登录成功，否则登录失败
            if (user.getPassword()!=null&&password.equals(user.getPassword())){
                return "2330210501辉,恭喜你登录成功";
            }
            else {
                return "密码错误";
            }
        }
        else {
            return "用户不存在";
        }
    }
    @RequestMapping("/reg")
    public String reg(String username,String password){
        QueryWrapper<User> query= Wrappers.query(User.class);
        query.eq("username",username);

        //根据用户名查询该用户是否存在 select* from user where username=?
        User user = userMapper.selectOne(query);
        if(Objects.nonNull(user)){
            return "用户存在";
        }
        user=new User();
        user.setUsername(username);
        user.setPassword(password);
        int i=userMapper.insert(user);
        return i>0?"注册成功":"注册失败";
    }

}
